package org.shagin.jfin.data.util;

import java.util.Currency;
import java.util.Locale;

import org.shagin.jfin.data.Money;

/**
 * The Class MoneyUtil.
 */
public class MoneyUtil {

    /**
     * Adds the.
     * 
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @return the money
     */
    public static Money add(Money arg1, Money arg2) {
        if (!arg1.getCurrency().equals(arg2.getCurrency()))
            throw new RuntimeException("Currency have to be equals");
        return new Money(arg1.getCurrency(), arg1.getValue() + arg2.getValue());
    }

    /**
     * Adds the.
     * 
     * @param arg1 the arg1
     * @param arg2 the arg2
     * @param currency the currency
     * @return the money
     */
    public static Money add(Money arg1, Money arg2, Currency currency) {

        if (!arg1.getCurrency().equals(currency))
            arg1 = exchange(arg1, currency);

        if (!arg2.getCurrency().equals(currency))
            arg2 = exchange(arg2, currency);

        return new Money(currency, arg1.getValue() + arg2.getValue());
    }

    /**
     * Exchange.
     * 
     * @param money the money
     * @param currency the currency
     * @return the money
     */
    public static Money exchange(Money money, Currency currency) {
        throw new RuntimeException();
    }

    /**
     * Zero.
     * 
     * @param currency the currency
     * @return the money
     */
    public static Money zero(Currency currency) {
        return new Money(currency, 0d);
    }

    /**
     * Zero default.
     *
     * @return the money
     */
    public static Money zeroDefault() {
        return new Money(Currency.getInstance(Locale.getDefault()), 0d);
    }

    /**
     * Format.
     * 
     * @param money the money
     * @return the string
     */
    public static String format(Money money) {
        return CurrencyUtil.format(money.getCurrency(), money.getValue());
    }

}
